package com.xlh.outside;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

import com.impl.SystemCore.Hander;
import com.wzm.param.ApiResultPo.AddUserReslult;
import com.wzm.param.ApiResultPo.ServerListsReslult;
import com.xlh.Utils.FUti;
import com.xlh.Utils.GsonManager;
import com.xlh.conf.CommonConf;
import com.xlh.interf.IUiUpdateHandler;
import com.xlh.lib.Core.FIntent;
import com.xlh.thread.TestTask;

import java.util.Map;

/**
 * Created by mike on 2017-12-19.
 */

public class RegisterActivity extends  BaseActivity implements IUiUpdateHandler {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.register);
        //Thread th = new Thread(new TestTask(this));
        //th.start();

        bindControl();
        ininControlEvent();

    }



    private void bindControl(){
        cEditUeseName = (EditText)findViewById(R.id.edt_user_reg_username);
       // cEditNickName = (EditText)findViewById(R.id.edt_user_reg_nickname);
        cEditPassword = (EditText)findViewById(R.id.edt_user_reg_pwd);
        cEditPhone    =  (EditText)findViewById(R.id.edt_user_reg_mobie);
        cbutSubmit      = (Button)findViewById(R.id.reg_but_submit);
        cButback      = (Button)findViewById(R.id.reg_but_back);
       // cRadio        = (RadioGroup)findViewById(R.id.reg_rd_sex);

        //默认值
       // setSex("男性");


    }


    @Override
    public Handler getHandler() {
        return handler;
    }

    /**
     * 初始化事件
     */
    private void ininControlEvent(){





        handler = new Handler(){

            @Override
            public void handleMessage(Message msg) {

                String res  = msg.getData().getCharSequence("result").toString();
                AddUserReslult data = GsonManager.getIns().jsonToAnyObject(res,AddUserReslult.class);
                if(data.getCode()==1){
                    Toast.makeText(RegisterActivity.this, "注册成功!", Toast.LENGTH_SHORT).show();
                    returnLogin();
                }else{
                    Toast.makeText(RegisterActivity.this, data.getMsg(), Toast.LENGTH_SHORT).show();
                }
                super.handleMessage(msg);
            }
        };






        //back Button
        cButback.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                returnLogin();
            }
        });



        //提交按钮
        cbutSubmit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String userName,password,phone,param;
                userName = cEditUeseName.getText().toString();

                password = cEditPassword.getText().toString();
                phone    = cEditPhone.getText().toString();
                param = String.format("username=%s&password=%s&phone=%s", userName,password,phone);




            Thread th = new Thread(new TestTask()
                    .setContext(RegisterActivity.this)
                    .setUrl(CommonConf.API_USER_REG + param)
                   );

            th.start();
        }
        });
    }


    public void returnLogin(){
        //进入相关游戏服
        FIntent fIntent = new FIntent(RegisterActivity.this, LoginActivity.class);
        startActivity(fIntent);
        finish();
    }




    /**
     * 用户名控件
     */
    private EditText cEditUeseName;
    private EditText cEditNickName;
    private EditText cEditPassword;
    private EditText cEditPhone;
    private Button cbutSubmit;
    private Button cbutReset;
    private Button cButback;
    private RadioGroup cRadio;

    private Handler handler;








}
